# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: 生产者消费者模型.py
Author: kij
Create Date: 2021/3/18
-------------------------------------------------
"""
import time
from multiprocessing import Queue, Process


# 生产者
def producter(name, q):
	for item in range(10):
		res = '%s %s ' % (name, item)
		time.sleep(0.5)
		print('%s生产出来了'%res)
		q.put(res)


# 消费者
def consumer(name, q):
	while True:
		res = q.get()
		if res is None: break
		time.sleep(1)
		print('消费者%s%s' % (name, res))


if __name__ == '__main__':
	q = Queue()
	p1 = Process(target=producter, args=('苹果', q))
	p2 = Process(target=producter, args=('香蕉', q))
	p3 = Process(target=producter, args=('橘子', q))
	c1 = Process(target=consumer, args=('孙悦', q))
	c2 = Process(target=consumer, args=('易建联', q))
	p1.start()
	p2.start()
	p3.start()
	c1.start()
	c2.start()
	p1.join()
	p2.join()
	p3.join()
	q.put(None)
	q.put(None)
	print('主进程')
